//
//  MJStatuePictureView.swift
//  新浪微博
//
//  Created by MaJi on 16/4/13.
//  Copyright © 2016年 MaJi. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
//对图片进行流水布局,使用UICollectionView
//图片间的距离(一行最多有三张图片)
let collectionCellID = "collectionCellID"
private let pictureCellMargin:CGFloat = 3
private let maxWidth:CGFloat = screenWidth - 2*StatusCellMargin
private let itemWidth:CGFloat = (maxWidth-2*pictureCellMargin)/3
class MJStatuePictureView: UICollectionView {

//    绑定数据 照片是一个数组
    var statusPicture:[MJStatusPicture]? {
        didSet{
//            print(statusPicture?.count)

//            更新数量
            labelCount.text = "\(statusPicture?.count ?? 0)"
//            根据图片的数量,设定view的大小
            let pSize = caculatePictureViewSize()
//            自动布局
            self.snp_updateConstraints { (make) -> Void in
//         更新 UICollectionView的大小.
                make.size.equalTo(pSize)
            }
//            更新页面数据
            reloadData()
        }
    }
    lazy var labelCount:UILabel = UILabel(text: "", textColor: UIColor.redColor(), fontSize: 14)
    func caculatePictureViewSize() -> CGSize{
        let picCount = statusPicture?.count ?? 0
        if picCount == 0 {
//            没图片
            return CGSizeZero
        }
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
//        有两个特殊的一张和四张
        if picCount == 1 {
            let imageSize = CGSize(width: 100, height: 100)
            //将layout的itemsize设置和配图一般大
            layout.itemSize = imageSize
            return imageSize
        }
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        if picCount == 4 {
            let w = itemWidth * 2 + pictureCellMargin
            return CGSize(width: w, height: w)
        }
//        剩下的就可以
        let row = Int(picCount-1)/3+1
//        let w = maxWidth
        let h = CGFloat(row)*itemWidth + (CGFloat(row) - 1)*pictureCellMargin
        return CGSize(width: maxWidth, height: h)
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
//        流水布局
        let flowLayout = UICollectionViewFlowLayout()
        super.init(frame: frame, collectionViewLayout: flowLayout)
//        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
//        布局子控件
        setupUI()
//        设置数据源代理,注册cell
        self.registerClass(PictureCell.self, forCellWithReuseIdentifier: collectionCellID)
        self.dataSource = self
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        addSubview(labelCount)
        self.backgroundColor = UIColor.whiteColor()
        labelCount.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
    }
}
//MARK实现数据源方法:有必须实现的方法,需要实现
extension MJStatuePictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return statusPicture?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(collectionCellID, forIndexPath: indexPath) as! PictureCell
//        cell.backgroundColor = randomColor()
        cell.statusPicture = self.statusPicture?[indexPath.row]
        return cell
    }
}

//自定义cell
class PictureCell:UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var statusPicture:MJStatusPicture? {
        didSet{
           let url = NSURL(string: statusPicture?.thumbnail_pic ?? "")
           iconView.sd_setImageWithURL(url)
        }
    }
    
    func setupUI(){
        addSubview(iconView)
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    
//    懒加载图片
    lazy var iconView:UIImageView = {
       let image = UIImageView()
//       设置填充模式
        image.contentMode = .ScaleAspectFill
        //手写代码需要手动设置裁剪
        image.clipsToBounds = true
        return image
    }()
}
